package socket;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import org.w3c.dom.ls.LSOutput;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * 聊天室的服务器
 */
public class Server {
    private ServerSocket serverSocket;  //总机
    public Server(){
        try {
            System.out.println("正在启动服务器");
            serverSocket = new ServerSocket(8088);
            System.out.println("服务器启动完成");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start(){
        Socket socket = null;
        try {
            while (true){  //循环监听客户端
                System.out.println("等待客户端连接...");
                socket = serverSocket.accept();  //分机
                System.out.println("一个客户端连接了！");

                InputStream in = socket.getInputStream();  //获取网络输入流
                int d;
                while ((d = in.read())!=-1){
                    System.out.print((char)d);
                }
                System.out.println();  //换行
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static void  main(String[] args){
        Server server = new Server();
        server.start();
    }
}
